package org.ocean.general.sample.conditional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class JoinSqls {
    private List<Creator> sqls;

    public List<Creator> getSqls() {
        return sqls;
    }

    public void setSqls(List<Creator> sqls) {
        this.sqls = sqls;
    }

    public JoinSqls(Builder builder) {
        this.sqls = builder.sqls;

    }
    public static Builder builder() {
        return new Builder();
    }
    public static class Builder{
        private List<Creator> sqls;

        public List<Creator> getSqls() {
            return sqls;
        }

        public void setSqls(List<Creator> sqls) {
            this.sqls = sqls;
        }

        public Builder(){
            this.sqls = new ArrayList<>();
        }

        public JoinSqls build(){
            return new JoinSqls(this);
        }

        public Builder andConditional(String key, Object value, String... alias){
            if(Objects.nonNull(key)){
                Creator creator = new Creator();
                creator.setCondition(true);
                creator.setOriginKey(key);
                creator.setValue(Arrays.asList(value));
                if(Objects.nonNull(alias)){
                    creator.setOriginAlias(alias[0]);
                }
                this.sqls.add(creator);
            }
            return this;
        }

        public Builder andRelation(String originKey,String originAlias,String targetKey,String targetAlias){
            if(Objects.nonNull(originKey) && Objects.nonNull(targetKey)){
                Creator creator = new Creator();
                creator.setCondition(false);
                creator.setOriginKey(originKey);
                creator.setOriginAlias(originAlias);
                creator.setTargetKey(targetKey);
                creator.setTargetAlias(targetAlias);
                this.sqls.add(creator);
            }
            return this;
        }
    }
    public static class Creator{
        private String originKey;
        private Iterable value;
        private String originAlias;
        private String targetKey;
        private String targetAlias;
        private boolean condition;

        public Iterable getValue() {
            return value;
        }

        public void setValue(Iterable value) {
            this.value = value;
        }

        public String getOriginKey() {
            return originKey;
        }

        public void setOriginKey(String originKey) {
            this.originKey = originKey;
        }


        public String getOriginAlias() {
            return originAlias;
        }

        public void setOriginAlias(String originAlias) {
            this.originAlias = originAlias;
        }

        public String getTargetKey() {
            return targetKey;
        }

        public void setTargetKey(String targetKey) {
            this.targetKey = targetKey;
        }


        public String getTargetAlias() {
            return targetAlias;
        }

        public void setTargetAlias(String targetAlias) {
            this.targetAlias = targetAlias;
        }

        public boolean isCondition() {
            return condition;
        }

        public void setCondition(boolean condition) {
            this.condition = condition;
        }
    }
}
